package com.ht.aems.fesmodel;

import com.ht.aems.fesmodel.persist.FilePersistence;
import com.ht.aems.fesmodel.persist.MeasFilePersist;
import com.ht.aems.fesmodel.persist.MeasSqlPersist;
import com.ht.aems.fesmodel.persist.SqlPersistence;
import org.apache.log4j.Logger;

import java.util.Arrays;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2007-3-26
 *         Time: 18:50:55
 */
public class FesPersistenceFactory {

    private static Logger log = Logger.getLogger(FesPersistenceFactory.class);

    private static String database;
    private static String type;

    static {
        if(FesProperties.getProperty(FesProperties.FES_DB_NAME) != null)
            database = FesProperties.getProperty(FesProperties.FES_DB_NAME).trim();
        if(FesProperties.getProperty(FesProperties.FES_PERSISTENCE_TYPE) != null)
            type = FesProperties.getProperty(FesProperties.FES_PERSISTENCE_TYPE);
    }

    public static FesPersistence createPersistence() {
        if (type.equalsIgnoreCase("database") || type.equalsIgnoreCase("sql")) {
            try {
                return new SqlPersistence(database);
            } catch (Exception e) {
                log.warn(Arrays.toString(e.getStackTrace()));
                return null;
            }
        } else if (type.equalsIgnoreCase("file")) {
            return new FilePersistence();
        }
        log.warn("no such type persistence of fes!");
        return null;
    }

    public static MeasPersistence createMeasPersistence() {
       if (type.equalsIgnoreCase("database") || type.equalsIgnoreCase("sql")) {
            try {
                return new MeasSqlPersist(database);
            } catch (Exception e) {
                log.warn(Arrays.toString(e.getStackTrace()));
                return null;
            }
        } else if(type.equalsIgnoreCase("file"))
            return new MeasFilePersist();
        log.warn("no such type persistence of fes!");
        return null;
    }

    public static String getDatabase() {
        return database;
    }

    public static void setDatabase(String database) {
        FesPersistenceFactory.database = database;
    }

    public static String getType() {
        return type;
    }

    public static void setType(String type) {
        FesPersistenceFactory.type = type;
    }
}
